package com.devbobcorn.nekoration.common.event;

import java.util.Iterator;
import java.util.List;
import java.util.function.Predicate;

import com.devbobcorn.nekoration.entities.ElectricArcEntity;
import com.devbobcorn.nekoration.entities.ModEntityType;

import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityClassification;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.MutableBoundingBox;
import net.minecraft.util.text.ITextComponent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.ServerChatEvent;
import net.minecraftforge.eventbus.api.Event;
import net.minecraftforge.eventbus.api.Event.Result;
import net.minecraftforge.eventbus.api.SubscribeEvent;

public class AbilityEvents {
	@SubscribeEvent
    public static void railgun(ServerChatEvent event) {
        if(event.getMessage().equals("RAILGUNMISAKA")) {
            event.setCanceled(true);
            PlayerEntity player = event.getPlayer();
            if (!player.abilities.invulnerable || !player.abilities.instabuild) return;
            EventMisaka ev = new EventMisaka(player);
            MinecraftForge.EVENT_BUS.post(ev);
            if(ev.getResult() == Result.ALLOW) {
                int radius = 40;
                List<Entity> list = player.level.getEntities(player, AxisAlignedBB.of(new MutableBoundingBox((int)player.xo - radius, (int)player.yo - radius, (int)player.zo - radius, (int)player.xo + radius, (int)player.yo + radius, (int)player.zo + radius)), MONSTER);
                for(Iterator<Entity> iterator = list.iterator();iterator.hasNext();){
                    Entity entity = iterator.next();
                    ElectricArcEntity lightning = new ElectricArcEntity(ModEntityType.$ELECTRIC_ARC_TYPE.get(), player.level);
                    lightning.setPos(entity.xo, entity.yo - 1, entity.zo);
                    player.level.addFreshEntity(lightning);
                }
            }
        }
    }

	public static final Predicate<Entity> MONSTER = new Predicate<Entity>() {
		@Override
		public boolean test(Entity s) {
			return s instanceof LivingEntity && (s.getClassification(false) == EntityClassification.MONSTER);
		}
	};
 
	public static class EventMisaka extends Event {
		public final PlayerEntity player;

		public EventMisaka(PlayerEntity p){
			super();
			this.player = p;
		}
	}

    @SubscribeEvent
    public static void misaka(EventMisaka event) {
		event.player.sendMessage(ITextComponent.nullToEmpty("君の指先を舞ってる電光は、私の一生変わらない信仰であり、このレールガンだけが永遠に生きてる!"), event.player.getUUID());
        event.setResult(Result.ALLOW);
    }
}
